#!/bin/sh

if /usr/sbin/laptop-detect; then LAPTOP=1; fi
CPUINFO=/proc/cpuinfo
IOPORTS=/proc/ioports

if [ ! -f $CPUINFO ] ; then
    echo $CPUINFO not detected... >2
    exit 1
fi

MODEL_NAME=`grep '^model name' "$CPUINFO" | head -1 | sed -e 's/^.*: //;'`
CPU=`grep -E '^cpud[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;'`
VENDOR_ID=`grep -E '^vendor_id[^:]+:' "$CPUINFO" | head -1 | sed -e 's/^.*: //;'`
CPU_FAMILY=$(sed -e '/^cpu family/ {s/.*: //;p;Q};d' $CPUINFO)

MODULE=none
MODULE_FALLBACK=acpi-cpufreq

# Two modules for PIII-M depending the chipset.
# modprobe speedstep-ich$EXT || modprobe speestep-smi$EXT  would be another way
if [ -f $IOPORTS ] && grep -q 'Intel .*ICH' $IOPORTS ; then
  PIII_MODULE=speedstep-ich
else
  PIII_MODULE=speedstep-smi
fi

case "$VENDOR_ID" in
    GenuineIntel*)
    # If the CPU has the est flag, it supports enhanced speedstep and should
    # use the speedstep-centrino driver
    if [ "`grep est $CPUINFO`" ]; then
	MODULE=speedstep-centrino;
    elif [ $CPU_FAMILY = 15 ]; then
    # Right. Check if it's a P4 without est.
	# Could be speedstep-ich, or could be p4-clockmod. 
	MODULE=speedstep-ich;
	# Disabled for now - the latency tends to be bad enough to make it
	# fairly pointless. 
	# echo "FREQDRIVER=p4-clockmod" >/etc/default/powernowd
	# to override this
#	if [ $LAPTOP = "1" ]; then
#	    MODULE_FALLBACK=p4-clockmod;
#	fi
    else
    # So it doesn't have Enhanced Speedstep, and it's not a P4. It could be 
    # a Speedstep PIII, or it may be unsupported. There's no terribly good
    # programmatic way of telling.
	case "$MODEL_NAME" in
	    Intel\(R\)\ Pentium\(R\)\ III\ Mobile\ CPU*)
	    MODULE=$PIII_MODULE ;;
	    
        # JD: says this works with   cpufreq_userspace
	    Mobile\ Intel\(R\)\ Pentium\(R\)\ III\ CPU\ -\ M*)
	    MODULE=$PIII_MODULE ;;
	    
        # https://bugzilla.ubuntu.com/show_bug.cgi?id=4262
        # UNCONFIRMED
	    Pentium\ III\ \(Coppermine\)*)
	    MODULE=$PIII_MODULE
	    ;;
	esac
    fi
    ;;
    AuthenticAMD*)
    # Hurrah. This is nice and easy.
    case $CPU_FAMILY in
	5)
	# K6
	MODULE=powernow-k6
	;;
	6)
	# K7
	MODULE=powernow-k7
	;;
	15|16|17)
	# K8
	MODULE=powernow-k8
	;;
    esac
    ;;
    CentaurHauls*)
    # VIA
    # If the CPU has the est flag, it supports enhanced powersaver and should
    # use the e_powersaver driver
    if [ "`grep est $CPUINFO`" ]; then
        MODULE=e_powersaver;
    elif [ $CPU_FAMILY = 6 ]; then
	MODULE=longhaul;
    fi
    ;;    
    GenuineTMx86*)
    # Transmeta
    if [ "`grep longrun $CPUINFO`" ]; then
	MODULE=longrun
    fi
    ;;    
esac
